﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace XLDL.Admin
{
    public partial class Them_TT : System.Web.UI.UserControl
    {
        string id = "";
        string[] size_array = { "" };
        protected void Page_Load(object sender, EventArgs e)
        {
            id= Request.QueryString["id"].ToString();
            Xuat_Size(int.Parse(id));
            Xuat_Hinh(int.Parse(id));
            Drop_ID_SP(Drop_id);
        }
        protected void Xuat_Size(int id)
        {
          //  string id = Request.QueryString["id"].ToString();
            DataTable tb = new DataTable();
            string lenh = "Select * from V_size where id_sp="+id+" order by so_size asc";
            tb = XLDL.XLDaTa(lenh);
            Luoi_size.DataSource = tb;
            Luoi_size.DataBind();
            lbl_thongbao.Visible = false;
            if (tb.Rows.Count == 0)
            {
                lbl_thongbao.Visible = true;
                lbl_tensp.Text = "";
            }
            else
            {
                this.lbl_tensp.Text = "Tên: " + tb.Rows[0]["ten_sp"].ToString();
            }
        }
        protected void Xuat_Hinh(int id)
        {
            //  string id = Request.QueryString["id"].ToString();
            DataTable tb = new DataTable();
            string lenh = "Select * from hinh_anh where id_sp=" + id + " order by id_hinh desc";
            tb = XLDL.XLDaTa(lenh);
            Luoi_hinh.DataSource = tb;
            Luoi_hinh.DataBind();
           // lbl_thongbao.Visible = false;
            //if (tb.Rows.Count == 0)
            //{
            //    lbl_thongbao.Visible = true;
            //    lbl_tensp.Text = "";
            //}
            //else
            //{
            //    this.lbl_tensp.Text = "Tên: " + tb.Rows[0]["ten_sp"].ToString();
            //}
        }
        protected void Drop_ID_SP(DropDownList drop)
        {
            DataTable tb = new DataTable();
            string lenh = "select * from san_pham order by id_sp desc";
            tb = XLDL.XLDaTa(lenh);
            Drop_id.DataSource = tb;
            Drop_id.DataTextField = "id_sp";
            Drop_id.DataValueField = "id_sp";
            Drop_id.DataBind();
            string id = Request.QueryString["id"].ToString();
            for (int i = 0; i < drop.Items.Count; i++)
            {
                if (id == drop.Items[i].ToString())
                {
                    drop.SelectedIndex = i;
                    this.lbl_tensp.Text = "Tên: " + tb.Rows[i]["ten_sp"].ToString();
                    break;
                }
            }
        }
       
        protected void Button_tim_Click(object sender, EventArgs e)
        {
            //Xuat_Size(int.Parse(Drop_id.SelectedItem.ToString()));
            //Xuat_Hinh(int.Parse(Drop_id.SelectedItem.ToString()));
            Response.Redirect("CTadm.aspx?act=Them_tt&id=" + Drop_id.SelectedItem.ToString() + "");
        }

        protected void Button_them_Click(object sender, EventArgs e)
        {
            DataTable tb = new DataTable();
            string size="";
            if(txt_size.Text!="")
            {
                size=txt_size.Text.Trim();
            }
            else
            {
                size=Drop_size.SelectedItem.ToString();
            }
            string lenh = "insert into size(id_sp,so_size) values("+int.Parse(Drop_id.SelectedItem.ToString())+",'"+size+"')";
            tb = XLDL.XLDaTa(lenh);
            Xuat_Size(int.Parse(Drop_id.SelectedItem.ToString()));
            lbl_id_size.Text = "";
        }

        protected void Luoi_size_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Xuat_Size(int.Parse(Drop_id.SelectedItem.ToString()));
            bool ok = false;
            lbl_id_size.Text = Luoi_size.Rows[e.NewSelectedIndex].Cells[0].Text;
            string so_size = Luoi_size.Rows[e.NewSelectedIndex].Cells[2].Text;
            for (int i = 0; i < Drop_size.Items.Count; i++)
            {
                if (so_size == Drop_size.Items[i].ToString())
                {
                    Drop_size.SelectedIndex = i;
                    txt_size.Text = "";
                    ok = true;
                    break;
                }
            }
            if (ok == false)
            {
                txt_size.Text= so_size;
            }
          
          //  string[] size_array = {""};
            //for (int i = 0; i < CheckBoxList_size.Items.Count; i++)
            //{
            //    if (so_size == CheckBoxList_size.Items[i].ToString())
            //    {
            //        CheckBoxList_size.Items[i].Selected = true;
            //        size_array[i] = CheckBoxList_size.Items[i].ToString();
            //    }
            //}
        }

        protected void Button_xoa_Click(object sender, EventArgs e)
        {
            DataTable tb = new DataTable();
            if (lbl_id_size.Text != "")
            {
                tb = XLDL.XLDaTa("delete from Size where id_size=" + int.Parse(lbl_id_size.Text) + "");
                Xuat_Size(int.Parse(Drop_id.SelectedItem.ToString()));
                lbl_id_size.Text = "";
                txt_size.Text = "";
            }
            else
            {
                lbl_thongbao.Text = "Vui vòng chọn Size để xoá !";
                lbl_thongbao.Visible = true;
            }
        }

        protected void Button_sua_Click(object sender, EventArgs e)
        {
            string size="";
            if(txt_size.Text!="")
            {
                size=txt_size.Text;
            }
            else
            {
                size=Drop_size.SelectedItem.ToString();
            }
            if (lbl_id_size.Text != "")
            {
                DataTable tb = new DataTable();
                string lenh = "update size set so_size='" + size + "' where id_size=" + int.Parse(lbl_id_size.Text) + "";
                tb = XLDL.XLDaTa(lenh);
                Xuat_Size(int.Parse(Drop_id.SelectedItem.ToString()));
            }
            else
            {
                lbl_thongbao.Text = "Vui lòng chọn Size để sửa !";
                lbl_thongbao.Visible = true;
            }
        }

        protected void Button_Size_Click(object sender, EventArgs e)
        {

        }

        protected void Luoi_hinh_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void Button_upload_Click(object sender, EventArgs e)
        {
            try
            {
                // Get the HttpFileCollection
                string path = System.Configuration.ConfigurationManager.AppSettings["Images_Path"];
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath(path) + System.IO.Path.GetFileName(hpf.FileName));
                        //Response.Write("<b>File: </b>" + hpf.FileName + "  <b>Size:</b> " +  hpf.ContentLength + "  <b>Type:</b> " + hpf.ContentType + " Uploaded Successfully <br/>");

                        DataTable tb = new DataTable();
                        string lenh = "insert into hinh_anh(id_sp,hinh_anh_2,hien_thi) values(" + int.Parse(Drop_id.SelectedItem.ToString()) + ",'" + hpf.FileName + "','true')";
                        tb = XLDL.XLDaTa(lenh);

                       lbl_tb.Text="Đã upload thành công";
                    }
                }
                Response.Redirect("CTadm.aspx?act=Them_tt&id=" + Drop_id.SelectedItem.ToString() + "");
            }
            catch (Exception ex)
            {

            }
        }


    }
}